import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class Main {

    /**
     * 信号量，控制多个线程访问有限的资源
     */
    public static void main(String[] args) {

        Semaphore semaphore = new Semaphore(5);

        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName()+"抢到车位了");
                    TimeUnit.MILLISECONDS.sleep(4000);
                    semaphore.release();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            },String.valueOf(i)).start();
        }

        System.out.println("完成了");
    }
}